﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Hawaii.Services.Client;
using Hawaii.Services.Client.Relay;


namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        private const string ApplicationName = "629_Poll_App";

        public MainPage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets the relay data context.
        /// </summary>
        private RelayContext RelayContext
        {
            get { return ((App)App.Current).RelayContext; }
        }

        /* this code will be called right after constructor */
        protected override void OnNavigatedTo(NavigationEventArgs nea)
        {
            if (String.IsNullOrEmpty(HawaiiClient.HawaiiApplicationId))
            {
                return;
            }

            /* create an endpoint */
            if (this.RelayContext.Endpoint == null)
            {
                RelayService.CreateEndPointAsync(HawaiiClient.HawaiiApplicationId,
                                                 MainPage.ApplicationName,
                                                 this.OnCompleteCreateEndPoint);
            }
            /* else, endpoint restored from relay service */

            ///* get user's name */
            if (((App)App.Current).fileStorage.df.login.name == "")
            {
                NavigationService.Navigate(new Uri("/Enter_Name.xaml", UriKind.Relative));
            }

            this.welcome.Text = "Welcome " + ((App)App.Current).fileStorage.df.login.name;

            base.OnNavigatedTo(nea);
        }

        /// <summary>
        /// Callback method for creating a new endpoint.
        /// </summary>
        /// <param name="result">Result object from service.</param>
        private void OnCompleteCreateEndPoint(EndpointResult result)
        {
            Debug.Assert(result != null, "result is null");

            if (result.Status == Status.Success)
            {
                // Set the newly created endpoint available in result as active(my) endpoint.
                this.RelayContext.Endpoint = result.EndPoint;

                this.Dispatcher.BeginInvoke(
                    delegate
                    {
             //           this.textBlock1.Text = "New Endpoint : " + result.EndPoint.RegistrationId;
                    });
            }
            else
            {
                // Display a message box, in case of any error creating a new endpoint.
                this.DisplayMessage("Error creating a new endpoint.", "Error");
            }

        }

        /// <summary>
        /// Helper method to display messages.
        /// </summary>
        /// <param name="message">A valid message.</param>
        /// <param name="caption">A valid caption.</param>
        private void DisplayMessage(string message, string caption)
        {
            this.Dispatcher.BeginInvoke(
                    delegate
                    {
                        MessageBox.Show(message, caption, MessageBoxButton.OK);
                    });
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
                        
        }

        private void View_Group_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PivotPage1.xaml?", UriKind.Relative));
        }

        private void View_Poll_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PivotPage1.xaml", UriKind.Relative));

        }

        private void Change_Name_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Enter_Name.xaml", UriKind.Relative));
        }

    }
}
